const express = require('express');
const router = express.Router();

const bodyParser = require('body-parser');

//10086  到下一个10086间的是我修改的内容
const mysql = require('mysql')
const accountSQL = require('../db/accountSQL');
const appealSQL = require('../db/appealSQL');
const dbConfig = require('../db/dbConfig');
const managerSQL = require('../db/managerSQL');
const messageSQL = require('../db/messageSQL');
const replySQL = require('../db/replySQL');
const sectionSQL = require('../db/sectionSQL');
const topicSQL = require('../db/topicSQL');
const userSQL = require('../db/userSQL');


// 使用DBConfig.js的配置信息创建一个MySql链接池
var pool = mysql.createPool( dbConfig.mysql )
// 响应一个JSON数据
const responseJSON = function (res, ret) {
  if (typeof ret === 'undefined') {
    res.json({
      code: '-200',
      msg: '操作失败'
    })
  } else {
    res.json(ret)
  }
}
//10086

router.post('/addreply', bodyParser.json(), (req, res) => {
  pool.getConnection(function(err,connection){
    // 检查用户ID是否为空
    if (!req.body["userID"]
      || typeof req.body["userID"] != "number") {
      res.json({
        err: 1,
        message: '用户ID为空'
      })
    }
    // 检查用户名是否为空
    else if (!req.body["username"]
      || typeof req.body["username"] != "string"
      || !req.body["username"].length) {
      res.json({
        err: 1,
        message: '用户名为空'
      })
    } else if (!req.body["postID"]
      || typeof req.body["postID"] != "string"
      || !req.body["postID"].length) {
      res.json({
        err: 1,
        message: '主帖为空'
      })
    } else if (!req.body["content"]
      || typeof req.body["content"] != "string"
      || !req.body["content"].length) {
      res.json({
        err: 1,
        message: '回帖内容为空'
      })
    } else {
      let userID = req.body["userID"];
      let username = req.body["username"];
      let postID = req.body["postID"];
      let content = req.body["content"];
      // 下方改数据库操作代码
      res.json({
        err: 200,
        message: '回帖成功',
        userID,
        username,
        postID,
        content
      })
    }
  })
})

module.exports = router;
